package com.ld.control.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ld.control.business.equipment.mapper.EquipmentMethodTmpMapper;
import com.ld.control.business.equipment.model.EquipmentMethodTmpModel;
import com.ld.control.business.equipment.service.EquipmentService;
import com.ld.control.business.equipment.vo.external.EquipmentCommandSendParamReqVo;
import com.ld.control.business.equipment.vo.req.MethodDebugParamReqVo;
import com.ld.control.business.equipment.vo.req.MethodDebugReqVo;
import com.ld.control.business.equipment.vo.resp.EquipmentRespVo;
import com.ld.control.common.CommonConstant;
import com.ld.control.properties.LDProperties;
import com.ld.control.sysenum.EquipmentStatusEnum;
import com.ld.control.sysenum.MethodParamTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;

/**
 * 发送设备组装参数的工具类
 */
@Slf4j
@Component
public class EquipmentCommandSendUtils {

    @Autowired
    private EquipmentMethodTmpMapper equipmentMethodTmpMapper;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private LDProperties ldProperties;

    /**
     * 给设备发送指令
     * @param equipmentCommandSendParamReqVo
     * @return
     */
    public String sendEquipmentCommand(EquipmentCommandSendParamReqVo equipmentCommandSendParamReqVo) {
        Long equipmentId = equipmentCommandSendParamReqVo.getEquipmentId();
        Long processNodeExampleRunningId = equipmentCommandSendParamReqVo.getProcessNodeExampleRunningId();
        Long taskProcessNodeExampleId = equipmentCommandSendParamReqVo.getTaskProcessNodeExampleId();

        // 检测设备状态是否正常
        // 修改设备状态
        equipmentService.updateStatus(equipmentId, EquipmentStatusEnum.RUNNING.getStatus());
        // 获取设备信息
        EquipmentRespVo equipmentRespVo = equipmentService.getById(equipmentId);
        String sendDebugCommandUrl = ldProperties.getDrive().getSendCommandUrl();
        String reqUrl = "http://"+equipmentRespVo.getIp() + ":" + equipmentRespVo.getPort() + sendDebugCommandUrl;
        String jsonString = JSONObject.toJSONString(equipmentCommandSendParamReqVo);
        log.info("\n====================send_command_all====================\nRunningId->{}\nnodeId->{}\nparam:{}\nreqUrl->{}",
                processNodeExampleRunningId, taskProcessNodeExampleId, jsonString, reqUrl);
        log.info("\n====================send_command_method_param====================\n{}", JSONObject.toJSONString(equipmentCommandSendParamReqVo.getMethodParams()));
        String postResp = HttpUtils.sendPostConnectTimeout(reqUrl,
                jsonString, 10L, 20L);
        log.info("\n收到驱动返回：{}", postResp);
        return postResp;
    }


    public String sendEquipmentCommandDebug(EquipmentCommandSendParamReqVo equipmentCommandSendParamReqVo) {
        // 获取设备信息
        EquipmentRespVo equipmentRespVo = equipmentService.getById(equipmentCommandSendParamReqVo.getEquipmentId());
        String domain = "http://"+equipmentRespVo.getIp() + ":" + equipmentRespVo.getPort();
        String sendDebugCommandUrl = ldProperties.getDrive().getSendDebugCommandUrl();
        String equipmentCommandSendParamReqVoStr = JSONObject.toJSONString(equipmentCommandSendParamReqVo);
        log.info("\n================debug_send_command_req================\n {}", equipmentCommandSendParamReqVoStr);
        String postResp = HttpUtils.sendPostConnectTimeout(domain + sendDebugCommandUrl,
                equipmentCommandSendParamReqVoStr, 10L, 20L);
        log.info("\n================send_command_resp================\n{}", postResp);
        return postResp;
    }

    /**
     * 生成方法调试的参数
     * @param equipmentId
     * @param equipmentCode
     * @param equipmentMethodTmpName
     * @param methodDebugParamReqVoList
     * @return
     */
    public EquipmentCommandSendParamReqVo generateMethodDebugParamReqVo(Long equipmentId, String equipmentCode, String equipmentMethodTmpName,
                                                                        List<MethodDebugParamReqVo> methodDebugParamReqVoList) {
        EquipmentCommandSendParamReqVo equipmentCommandSendParamReqVo = new EquipmentCommandSendParamReqVo();
        equipmentCommandSendParamReqVo.setTaskProcessNodeExampleId(0L);
        equipmentCommandSendParamReqVo.setEquipmentId(equipmentId);
        equipmentCommandSendParamReqVo.setEquipmentCode(equipmentCode);
        equipmentCommandSendParamReqVo.setMethodName(equipmentMethodTmpName);
        equipmentCommandSendParamReqVo.setMessageId("debug_" + equipmentCode + "_" + equipmentMethodTmpName);
        JSONObject methodParam = new JSONObject();
        for (MethodDebugParamReqVo data : methodDebugParamReqVoList) {
            if (data.getUseType() == CommonConstant.USE_FLAG.RESP) {
                continue;
            }
            String valueStr = data.getValue();
            Object paramValue = CommonUtils.getParamValue(data.getParamType(), valueStr);
            methodParam.put(data.getName(), paramValue);
        }
        equipmentCommandSendParamReqVo.setMethodParams(methodParam);
        log.info("generateMethodDebugParamReqVo:{}", JSONObject.toJSONString(equipmentCommandSendParamReqVo));
        return equipmentCommandSendParamReqVo;
    }





}
